package com.changgou.seckill.controller;

import com.changgou.seckill.service.SeckillOrderService;
import entity.Result;
import entity.SeckillStatus;
import entity.StatusCode;
import entity.TokenDecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author:hushengjin
 * @date:2019-08-12 20:49:32
 */

@RestController
@RequestMapping("/seckillOrder")
@CrossOrigin
public class SeckillOrderController {

    @Autowired
    private SeckillOrderService seckillOrderService;
    @Autowired
    private TokenDecode tokenDecode;

    /**
     * 添加订单
     *
     * @param time
     * @param id
     * @return
     */
    @RequestMapping(value = "/add")
    public Result add(String time, Long id) {
        try {
            String username = tokenDecode.getUserInfo().get("username");
            Boolean bo = seckillOrderService.add(time, id, username);
            if (bo) {
                //抢单成功
                return new Result(true, StatusCode.OK, "抢单成功! ");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(true,StatusCode.ERROR,"服务器繁忙，请稍后再试");
    }

    @RequestMapping(value = "/query")
    public Result queryStatus() {
        //获取用户名
        String username = tokenDecode.getUserInfo().get("username");
        //根据用户名查询用户抢购状态
        SeckillStatus seckillStatus = seckillOrderService.queryStatus(username);
        if (seckillStatus != null) {
            return new Result(true, seckillStatus.getStatus(), "抢购状态", seckillStatus);
        }
        return new Result(false, StatusCode.NOTFOUNDERROR, "没有抢购信息");
    }
}
